require 'scrobbler'

#returns a user instance
def user login
  raise RuntimeException.new "Login shouldn't be null" unless login!=nil 
  Scrobbler::User.new(login)
end

#prints something
def show subject
  puts subject
end

#right now used to make things more readable
def by something
  return something
end

#brings the music for a given user
def music someone  
  LastLingo::UserMusicLibrary.new(someone);
end

#selects the played music
def played music_library
	music_library.top_played
end

#selects the toppest among a list
def top list
	list.sort.last
end

module LastLingo

  class UserMusicLibrary

    def initialize scrobblerUser
      raise RuntimeError.new "ScrobblerUser can't be nil" unless scrobblerUser!=nil
      @scrobblerUser=scrobblerUser
      @music_lists = []
      @music_lists =  recents() + top_played()
    end

    def recents
	scrobbler_tracks = @scrobblerUser.recent_tracks
	list = [] + ( !scrobbler_tracks.nil? ? scrobbler_tracks : [] )
	def list.sort
		self.sort{|a,b| a.date <=> b.date  }		
	end
	list
    end

    def top_played
	scrobbler_tracks = @scrobblerUser.top_tracks
       	list = [] + ( !scrobbler_tracks.nil? ? scrobbler_tracks : [] )

	def list.sort
		super{|a,b| a.playcount <=> b.playcount  }		
	end

	list
    end


    def size
	@music_lists.size
    end

  end

end
